/**
 * Copyright (c) 2011-9-23 WKM and/or its affiliates. 
 * Use is subject to license terms.
 * All rights reserved.
 */
package org.wkm.util;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * The class note
 * @version 1.0 2012-1-17
 * @author WUKUNMENG
 */
public class FileUtil {

	private FileUtil() {}
	
	//文件路径分隔符
	public static String separator = File.separator;
	
	//换行分隔符
	public static String lineSeparator = System.getProperty("line.separator");
	
	//多文件路径分隔符 Window-; Linux-:
	public static String pathSeparator = File.pathSeparator;
	
	//用户系统目录
	public static String userHome = System.getProperty("user.home");
	
	/**
	 * 创建新文件
	 * @param file
	 * @throws IOException
	 */
	public static void createFile(File file) throws IOException{
		if(!file.exists()){
			file.createNewFile();
		}
	}
	
	/**
	 * 复制文件
	 * @param from
	 * @param to
	 */
	public static void copyFile(File from,File to)throws IOException{
		if(from == null){
			return ;
		}
		
		if(!from.exists()){
			return ;
		}
		
		if(!to.exists()){
			to.createNewFile();
		}
		
		FileReader reader = new FileReader(from);
		FileWriter writer = new FileWriter(to);
		char[] buffer = new char[1024];
		int count = 0;
		while ((count = reader.read(buffer)) != -1){
			writer.write(buffer, 0, count);
		}
		reader.close();
		writer.close();
	}
	
	/**
	 * 扫描文件路径，返回指定名称的目录
	 * @param root 扫描的文件路径
	 * @param name 目录名称
	 * @return 返回指定的文件
	 * @throws IOException
	 */
	public static File scanFile(File root,String name)throws IOException{
		if(root.listFiles() == null){
			return null;
		}
		for(File file:root.listFiles()){
			if(file.getName().equals(name)){
				return file;
			} else {
				File tmp = scanFile(file,name);
				if(tmp != null){
					return tmp;
				}
			}
		}
		return null;
	}
}
